/***2024-11-20 迷途小羔羊
 * 用于编写角色身上的堆叠物体管理
*/
namespace ghost
{
    export class RoleEffectMgr implements IResource,IUpdate
    {
        protected _list:RoleEffect[];
        constructor()
        {
            this._list = [];
        }
		protected sortEffect(a:RoleEffect,b:RoleEffect):number
		{
			return a._sortId < b._sortId?-1:1;
		}
        /**重置堆叠顺序*/
        public resetRoleEffectFloat():void
		{
			let s =this;
			let len:number,toY:number;
			let roleEffect:RoleEffect;
			toY = 0;
			len = s._list.length;
			if(len > 0)
			{
				if(len > 1)
				{
					s._list.sort(s.sortEffect);
				}
				while(--len>-1)
				{
					roleEffect = s._list[len];
					if(!roleEffect.isValid())
					{
						s._list.splice(len, 1);
						continue;
					}					
					roleEffect.effect.setRoleFloatH(toY);
					toY += roleEffect.height;
				}
			}			
		}
		public addRoleEffect(effect:RoleEffect, remove:boolean=true):void
		{
			let s= this;
			if(remove)
				s.removeRoleEffectById(effect.id);
            effect.roleEffMgr = s;
			s._list[s._list.length] = effect;
			s.resetRoleEffectFloat();
		}
		public removeRoleEffect(effect:RoleEffect):void
		{			
			this.removeRoleEffectById(effect.id);
		}		
		public removeRoleEffectById(id:number):void
		{
			let s = this;
			let ind:number;
			ind = s.findRoleEffectIndexById(id);
			if(ind > -1)
			{
				s._list[ind].dispose();
				s._list.splice(ind, 1);
			}
		}		
		public findRoleEffectById(id:number):RoleEffect
		{						
			return this._list[this.findRoleEffectIndexById(id)];
		}
		public findRoleEffectIndexById(id:number):number
		{
			let s = this;
			let len:number;
			len = s._list.length;
			while(--len>-1)
			{
				if(s._list[len].id == id)				
					return len;	
			}
			return -1;
		}
		public clearRoleEffect():void
		{
			let s = this;
			let len:number;
			len = s._list.length;
			while(--len>-1)			
				s._list[len].dispose();
			s._list.length = 0;
		}
        public disposed: boolean;
        public dispose():void
        {
            let s= this;
            if(s.disposed)return;
            s.disposed = true;
            s.clearRoleEffect();
        }
		protected _invalidRoleEffect:boolean;
		public invalidRoleEffect():void
		{
			let s= this;
			if(s._invalidRoleEffect)return;
			s._invalidRoleEffect = true;
		}
		public validRoleEffect():void
		{
			let s = this;
            if(s.disposed)return;
			s.resetRoleEffectFloat();			
		}

        public updating: boolean;
        public updateView(): void
        {
            let s= this;
            if(s._invalidRoleEffect)
            {
                s.validRoleEffect();
                s._invalidRoleEffect = false;
            }
        }    
    }
}